class User < ActiveRecord::Base
  has_one(:updater)

  belongs_to(:role)

  has_and_belongs_to_many(:user_group)
  has_and_belongs_to_many(:editor_group)

  has_many(:user_address, :class_name => "UserAddress", :dependent => :destroy)
  has_many(:user_contact, :dependent => :destroy)

  validates_presence_of(:first_name, :last_name, :login)

  validates_uniqueness_of(:login)

  #Password validation done in /lib/user_data.rb since the password is encrypted
  #in the params object.
  validates_confirmation_of(:password)
 
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # Class methods
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  
  class << self
    def authenticate(login,pass)
      find(:first, :conditions => ["login = ? and password = ?", login, Security::Encryption.encrypt(pass)])
    end

    def search_for_values(values, role_id)
      extended_find(values,role_id)
    end

    private

      def extended_find(values,role_id)
        econditions = extended_conditions(values)
        restraints = ""
        restraints = "and (#{econditions})" if econditions.length > 0
        find_by_sql(<<-SQL)
          select u.* from users u where u.role_id = #{role_id}
          #{restraints}
        SQL
      end
                                    
      def extended_conditions(values)
        fields = %w(u.first_name u.last_name u.middle_name)
        conditions = Utils::Search.or_search_conditions(fields,values)
        return "( #{conditions} )" if conditions.length > 0
        return ""
      end

  end #end class methods

  def full_name_with_login
    first_name + " " + last_name + " ( #{login} )"
  end

  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # ACL Tests
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def access_user_group?
    sm = SystemMethod.find(:first, :conditions => "name = 'manage/permissions/create_user_group'")
    user_group.each do |ug|
      return true if ug.has_system_object_id?(sm.system_object_id)
    end
    role.has_system_object_id?(sm.system_object_id)
  end

  def access_job_management?
    sm = SystemMethod.find(:first, :conditions => "name = 'jobs/create'")
    user_group.each do |ug|
      return true if ug.has_system_object_id?(sm.system_object_id)
    end
    role.has_system_object_id?(sm.system_object_id)
  end

  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # Relationship methods
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  def find_contact_by_type(id)
    user_contact.each{|c|
      return c if c.user_contact_type_id == id.to_i
    }
    return nil
  end

  def find_address_by_type(id)
    user_address.each{|ua|
      return ua if ua.user_address_type_id == id.to_i
    }
    return nil
  end

  def remove_contact_by_type(id)
    uc = find_contact_by_type(id)
    user_contact.delete(uc) unless uc.nil?
  end

  def remove_address_by_type(id)
    ua = find_address_by_type(id)
    user_address.delete(ua) unless ua.nil?
  end

  def get_current_contact_value_by_type(id)
    user_contact.each{|c|
      return c.value if c.user_contact_type_id == id.to_i
    }
    return nil
  end

  def allowed_urls
    rvalue = []
    editor_group.each do |eg|
      eg.page.each do |pg|
        rvalue << {:controller => "/manage/pages", :action => "update" , :id => pg.id}
      end
      eg.document_category.each do |dc|
        rvalue << {:controller => "/manage/documents", :action => "manage_files", :id => dc.id}
        dc.document.each do |doc|
          rvalue << {:controller => "/manage/documents", :action => "manage_file_versions", :id => doc.id}
          rvalue << {:controller => "/manage/documents", :action => "delete_file", :id => doc.id}
        end
      end
    end
    rvalue
  end
end
